<?php
namespace Swiftx\Application;

/**
 * 模块错误类
 * @package Swiftx\Exception
 */
class Error extends \ErrorException {

    /**
     * 错误异常构造.
     * @param int $code
     * @param int $rank
     */
    public function __construct(int $code, int $rank=0) {
        $file = dirname(__DIR__).'/exception.json';
        $config = json_decode(file_get_contents($file), true);
        $key = 'error-'.str_pad($code,3,0,STR_PAD_LEFT);
        if(isset($config[$key])){
            $debug = debug_backtrace()[1+$rank];
            $message = '[ERROR '.str_pad($code,3,0,STR_PAD_LEFT).'] '.$config[$key];
        } else {
            $debug = debug_backtrace()[0];
            $message = '未知异常:'.$key.'请尽快进行异常说明！';
        }
        parent::__construct($message, $code, 1, $debug['file'], $debug['line']);
    }

}